package com.fernfx.xingtan.contacts.ui;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.fernfx.xingtan.ConsumerApplication;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.contacts.NewFriendsAdapter;
import com.fernfx.xingtan.contacts.contract.NewFriendsContract;
import com.fernfx.xingtan.contacts.entity.NewFriendsEntity;
import com.fernfx.xingtan.contacts.presenter.NewFriendsPresenter;
import com.fernfx.xingtan.main.adapter.SortAdapter;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.ui.ContactsInfoFragment;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.UIUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.fernfx.xingtan.main.ui.ContactsInfoFragment.OPERATION_CONTACTS_STATUS;

/**
 * Created by admin on 2018/8/6.
 * 新朋友
 */
public class NewFriendsActivity extends BaseActivity implements NewFriendsContract.View {
    @BindView(R.id.tv_refund_project_title)
    TextView refundProjectTv;

    @BindView(R.id.friends_info_liv)
    ListView friendsInfoLiv;

    @BindView(R.id.search_friends_edit)
    EditText searchFriendsEdit;

    private NewFriendsContract.Presenter presenter = new NewFriendsPresenter();

    private Handler inputHanlder = null;
    //handler wath
    public static final int INPUT_HANDLER_WATH = 1;
    //ContactsInfoFragment key
    public static final String REFURBISH_CALLBACK = "refurbish_callback";

    private ArrayList<ContactsInfoEntity.ObjBean.RecordsBean> contactsInfoList = null;

    public static void start(android.support.v4.app.Fragment fragment) {
        Intent intent = new Intent(fragment.getActivity(), NewFriendsActivity.class);
        fragment.startActivityForResult(intent,OPERATION_CONTACTS_STATUS);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_chat_append_friend;
    }

    @Override
    protected void initView() {
        setToolbarTitle("新的朋友");
        //设置点击发送样式
        /*refundProjectTv.setVisibility(View.VISIBLE);
        refundProjectTv.setTextColor(UIUtil.getColor(R.color.red));
        refundProjectTv.setText("发送");*/
    }

    @Override
    protected void initData() {
        //init
        presenter.init(this);
        //init handler
        inputHanlder = initInputHandler();
        //ContactsInfoFragment
        contactsInfoList = (ArrayList) getIntent().getSerializableExtra(REFURBISH_CALLBACK);
        //set fragment
        presenter.setContactsInfoList(contactsInfoList);
        //获取请求的好友列表
        getRequestFriends();
    }

    @Override
    protected void initListener() {
        searchFriendsEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputText = s.toString();
                if (TextUtils.isEmpty(inputText)) {
                    presenter.getUserRelationVOList().clear();
                    presenter.getUserRelationVOList().addAll(presenter.getCacheUserRelationVOList());
                    //刷新adapter
                    presenter.getNewFriendsAdapter().notifyDataSetChanged();
                    presenter.getNewFriendsAdapter().notifyDataSetInvalidated();
                    return;
                }
                if (CollectionUtil.isEmpty(presenter.getCacheUserRelationVOList())) {
                    return;
                }
                //send message handler
                Message msg = inputHanlder.obtainMessage();
                msg.what = INPUT_HANDLER_WATH;
                msg.obj = inputText;
                inputHanlder.sendMessageDelayed(msg, 500);
            }
        });
    }

    @Override
    public void getRequestFriends() {
        presenter.getRequestFriends();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
        if (null != inputHanlder) {
            inputHanlder.removeMessages(INPUT_HANDLER_WATH);
            inputHanlder = null;
        }
    }

    @Override
    public ListView getFriendsInfoLiv() {
        return friendsInfoLiv;
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    public Handler initInputHandler() {
        //init Handler
        Handler handler = new Handler(getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                String inputText = String.valueOf(msg.obj);
                List<NewFriendsEntity.ObjBean.UserRelationVOListBean> userRelationVOList = presenter.getCacheUserRelationVOList();
                //搜索list是否有输入的内容
                List<NewFriendsEntity.ObjBean.UserRelationVOListBean> inputUserRelationVOList = new ArrayList<>(5);
                for (int i = 0; i < userRelationVOList.size(); i++) {
                    NewFriendsEntity.ObjBean.UserRelationVOListBean userRelationVOListBean = userRelationVOList.get(i);
                    if (null == userRelationVOListBean) {
                        continue;
                    }
                    if (!TextUtils.isEmpty(userRelationVOListBean.getNickname()) && userRelationVOListBean.getNickname().contains(inputText)) {
                        inputUserRelationVOList.add(userRelationVOListBean);
                    }
                }
                //保存的size
                int userRelationVoSize = presenter.getUserRelationVOList().size();
                //重新添加
                presenter.getUserRelationVOList().clear();
                presenter.getUserRelationVOList().addAll(inputUserRelationVOList);
                //说明没有变不需要刷新
                if (userRelationVoSize == inputUserRelationVOList.size()) {
                    inputHanlder.removeMessages(INPUT_HANDLER_WATH);
                    return;
                }
                //刷新adapter
                presenter.getNewFriendsAdapter().notifyDataSetChanged();
                presenter.getNewFriendsAdapter().notifyDataSetInvalidated();
            }
        };
        return handler;
    }
}
